package com.unina.is2.astafantaunina.utility;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;

public class EliminaCalciatoreTask extends AsyncTask<String, Void, Void> {
	
	public static final String URL = "http://54.76.27.136/AndroidConnect";
	
	@Override
	protected Void doInBackground(String... lega) {
		// TODO: attempt authentication against a network service.
		
				
				
		//Il server deve connettersi ad openfire
		   	 DefaultHttpClient httpClient = new DefaultHttpClient();
		   	 HttpPost httpPost = new HttpPost(URL);
		   	 //diciamo al server che deve connettersi
		   	 List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		   	 pairs.add(new BasicNameValuePair("collegati", lega[1]));
		   	 //trasmettiamo anche il nome della lega selezionata
		   	 pairs.add(new BasicNameValuePair("lega", lega[0]));
		   	 
		   	 if (lega.length>2){
		   		 System.out.println("ELIMINA GIOCATORE");
		   		pairs.add(new BasicNameValuePair("giocatore", lega[2]));
		   		pairs.add(new BasicNameValuePair("utente", lega[3]));
		   	 }
		   	 
		   	 try {
					httpPost.setEntity(new UrlEncodedFormEntity(pairs));
					//Eseguiamo la POST
					httpClient.execute(httpPost);
				} catch (UnsupportedEncodingException e) {
		           e.printStackTrace();
		       } catch (ClientProtocolException e) {
		           e.printStackTrace();
		       } catch (IOException e) {
		           e.printStackTrace();
		       }
			return null;
	}		
		
	}
